<?php
namespace app\admin\controller;
use think\Controller;
use think\Controllers;

use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Bank extends Controllers{

	public function index(){
		$page = isset($_REQUEST['page'])?$_REQUEST['page']:0;
			$pindex    = max(1, intval($page));

		$psize     = 20;

		
		$wheres = " AND uniacid=2";

		if(!empty($_REQUEST['id'])){

			$id = $_REQUEST['id'];
			$wheres.=" and id=:id  ";

			
		}

		if(!empty($_REQUEST['bankname'])){

			$wheres.=" and bankname LIKE '%".$_REQUEST['name']."%'";

			
		}
		$sql = "SELECT * FROM ims_xuan_zy_bank WHERE  1 {$wheres}";

		$sql .= " limit " . ($pindex - 1) * $psize . ',' . $psize;

		$list = DB::query($sql);


		$total           = DB::query("select count(*) as count from ims_xuan_zy_bank WHERE 1 {$wheres} ")[0]['count'];
		// print_r($list);die;
		$pager           = $this->pagination($total, $pindex, $psize);
		$this->assign('list', $list);
    	return $this->fetch("admin/bank");

	}
	public function add(){
		
		if(request()->isget()){
			$id = isset($_GET['id'])?$_GET['id']:0;
			if($id!=0){
				$sql ="select * from ims_xuan_zy_bank where id ={$id}";
				$arr = DB::query($sql)[0];
				$arr['content'] = json_decode($arr['content'],true);
				$arr['fy'] = json_decode($arr['fy'],true);
				$this->assign("arr",$arr);	
			}
			return $this->fetch("admin/bank_add");

		} else {

			$data=array('xiangqing'=>$_POST['xiangqing'],'zhinan'=>$_POST['zhinan']);
			$_GPC['da']['ico'] = $_POST['ico'];
			$_GPC['da']['logo'] = $_POST['logo'];
			$_GPC['da']['content'] = json_encode($data);
			$fy = ["type"=>$_POST['type'],"sx"=>$_POST['sx']];
	        $_GPC['da']['fy'] = json_encode($fy);
			$_GPC['da']['createtime'] = time();
			$_GPC['da']['uniacid'] = 2;
			$_GPC['da']['bankname'] = $_POST['bankname'];
			$_GPC['da']['card_name'] = $_POST['card_name'];
			$_GPC['da']['vir_number'] = $_POST['vir_number'];
			$_GPC['da']['tag'] = $_POST['tag'];
			$_GPC['da']['jiangjin'] = $_POST['jiangjin'];
			$_GPC['da']['yjjieshao'] = $_POST['yjjieshao'];
			$_GPC['da']['tishi'] = $_POST['tishi'];
			$_GPC['da']['video'] = $_POST['video'];
			$_GPC['da']['url'] = $_POST['url'];
			$_GPC['da']['url2'] = $_POST['url2'];
			$_GPC['da']['poster_id'] = $_POST['poster_id'];
			$_GPC['da']['rank'] = $_POST['rank'];
			$_GPC['da']['status'] = $_POST['status'];
			$_GPC['da']['tuijian'] = $_POST['tuijian'];
			$_GPC['da']['status'] = $_POST['status'];
			if(!empty($_POST['id'])){
			DB::table('ims_xuan_zy_bank')->where("id","{$_POST['id']}")->setField($_GPC['da']);
			$this->success("修改成功","admin/bank/index");

			}else {

			DB::table('ims_xuan_zy_bank')->insert($_GPC['da']);
			$this->success("添加成功","admin/bank/index");
			}
		

		}

	}
	public function del(){
		$id = $_POST['id'];
		$sql ="delete from ims_xuan_zy_bank  where id = {$id}";
		DB::query($sql);
		return true;
	}
	public function img(){
	$page = isset($_REQUEST['page'])?$_REQUEST['page']:1;
	$limit = 10;
	$start = ($page-1)*$limit;
	$sql = "select * from ims_core_attachment order by createtime desc limit {$start},{$limit}";
	$list = DB::query($sql);
		if(request()->isget()){
		$sql = "select count(*) as count from ims_core_attachment order by createtime desc ";
		$all = DB::query($sql)[0]['count'];
		$this->assign("list",$list);
		$this->assign("all",$all);
		$this->assign("type",$_REQUEST['type']);
		return $this->fetch("admin/imgsel");
		} else{

		echo json_encode($list);
		}	

	}
	public function upload(){
		$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
		$ext = strtolower($ext);
		$url = "/public/static/images/2/2018/09/";
		$surl = "images/2/2018/09/";
		
		
		do {
		$filename = $this->random(10) . '.' . $ext;
		} while (file_exists($url . $filename));
		$upload =$_SERVER['DOCUMENT_ROOT']."/tp5".$url.$filename;
		$file = $_FILES["file"]["tmp_name"];
		$wj = file_get_contents($file);
		file_put_contents($upload, $wj);
   	    // move_uploaded_file($file,$upload);
         $img = $surl.$filename;
         $time = time();
         $sql = "insert into ims_core_attachment values(null,2,1,'{$_FILES['file']['name']}','{$img}',1,'{$time}','','-1')";
     	  DB::query($sql);
       
         	echo json_encode(["type"=>0,"data"=>$img]);
      

      
    }
    public function demo(){
    	$uid = 55588;
    	$sql = "select * from ims_xuan_zy_commission_team where uid  = '{$uid}'";
    	$user = DB::query($sql)[0];
    	$suser = DB::query("select * from ims_mc_members where uid = '{$user['suid']}'")[0];
    	$my = DB::query("select * from ims_mc_members where uid = '{$user['uid']}'")[0];
    	$ssuser = DB::query("select * from ims_mc_members where uid = '{$user['ssuid']}'")[0];
    	$team = DB::query("select * from ims_mc_members where uid = '{$user['team_id']}'")[0];
    	$partner = DB::query("select * from ims_mc_members where uid = '{$user['partner_id']}'")[0];
    	$this->assign("my",$my);
    	$this->assign("user",$user);
    	$this->assign("suser",$suser);
    	$this->assign("ssuser",$ssuser);
    	$this->assign("team",$team);
    	$this->assign("partner",$partner);
    	 return $this->fetch("admin/demo");

    	
    }
    public function ajaxdemo(){
    $uid = $_REQUEST['user_id'];
    $bank_name = "交通银行";
    $type = $_REQUEST['type'];
	$sql = "select * from ims_xuan_zy_commission_team where uid  = '{$uid}'";
	$user = DB::query($sql)[0];   
	$yj = $this->yj_tree($uid);
	$bank_yj = $yj['bank'][$bank_name];
	$yj_sum = array_sum($bank_yj);
	$team_yj = $this->com_tree($user['team_id'])[$bank_name];
	$team_money = $team_yj-$yj_sum;

	$partner_yj = $this->com_tree($user['partner_id'])[$bank_name];
	$partner_money  = $partner_yj - $team_yj;

		$sql  =  "update ims_mc_members set credit2=credit2+'{$bank_yj['one']}' where uid ='{$user['suid']}'";
		DB::query($sql);
 		$sql  =  "update ims_mc_members set credit2=credit2+'{$bank_yj['two']}' where uid ='{$user['ssuid']}'";
		DB::query($sql);
 		$sql  =  "update ims_mc_members set credit2=credit2+'{$bank_yj['three']}' where uid ='{$user['sssuid']}'";
		DB::query($sql);
 		$sql  =  "update ims_mc_members set credit2=credit2+'{$team_money}' where uid ='{$user['team_id']}'";
		DB::query($sql);
		$sql  =  "update ims_mc_members set credit2=credit2+'{$partner_money}' where uid ='{$user['partner_id']}'";
		DB::query($sql);
		$data['one'] = $bank_yj['one'];
		$data['two'] = $bank_yj['two'];
		$data['three'] = $bank_yj['three'];
		$data['team'] = $team_money;
		$data['partner'] = $partner_money;
		echo json_encode($data);
    }
    public function random($length=16){
    	  $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

        return static::substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
    }
      public static function substr($string, $start, $length = null)
    {
        return mb_substr($string, $start, $length, 'UTF-8');
    }

	


}